package com.CommunityHub.kiana.controller;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * http://localhost:8080/web/down
 */
@WebServlet("/down")
public class FIleUIploadController  extends HttpServlet {

    public static final String  BASE_DIR ="d:\\upload\\";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        String path = "pictures/d8bf7a3d-0516-4f17-ace5-733fcf496d99.jpg";
        String path = req.getParameter("filename");
        File file = new File(BASE_DIR,path);
        InputStream is = new FileInputStream(file);
        ServletOutputStream outputStream = resp.getOutputStream();

        byte[] buffer = new byte[1024];
        int len = 0;

        while ((len = is.read(buffer)) != -1) {
            outputStream.write(buffer,0,len);
            outputStream.flush();

        };
    }
}
